package com.wjjun.base.binding.adapter

import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.wjjun.base.utils.dip2dp

/**
 *
 * 写成顶层方法，这样相当于java中的静态方法。
 * author : wangjunjun
 * date   : 2022/6/2
 * desc   :
 */

class ImageViewAdapter {
    companion object {

        @JvmStatic
        @BindingAdapter("radius")
        fun setRadius(imageView: ImageView, radius: Float) {
            imageView.clipToOutline = true
            imageView.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View, outline: Outline) {
                    outline.setRoundRect(
                        0,
                        0,
                        imageView.width,
                        imageView.height,
                        view.dip2dp(radius)
                    )
                }
            }
            imageView.invalidate()
        }

        @JvmStatic
        @BindingAdapter("android:src")
        fun setRadius(imageView: ImageView, res: Int) {
            imageView.setImageResource(res)
        }

        @JvmStatic
        @BindingAdapter("url")
        fun setUrl(imageView: ImageView, url: String) {
            Glide.with(imageView.context).load(url).into(imageView)
        }
    }
}